﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinCSharp
{
    public partial class LinQ : Form
    {
        public LinQ()
        {
            InitializeComponent();
        }

        private void LinQ_Load(object sender, EventArgs e)
        {

        }

        //连接查询
        private void test()
        {
            string[] categories = new string[]{ 
                    "家电", 
                    "家具", 
                    "书籍", 
                    "手机", 
                    "成人用品" };

            List<Product> products = new List<Product>();
            products.Add(new Product { category = "家电", productName = "海尔电视机" });
            products.Add(new Product { category = "家电", productName = "电脑" });
            products.Add(new Product { category = "家具", productName = "床垫" });
            products.Add(new Product { category = "书籍", productName = "消费者行为学" });
            products.Add(new Product { category = "书籍", productName = "天天向上" });
            products.Add(new Product { category = "成人用品", productName = "伟哥" });
            products.Add(new Product { category = "成人用品", productName = "杰士邦" });
            products.Add(new Product { category = "农产品", productName = "黄瓜" });

            var q1 = from c in categories
                     join p in products on c equals p.category into ps
                     //from p in ps
                     select new { Category = c, tt = ps };

            foreach (var v in q1)
            {
                foreach (var t in v.tt)
                {
                    Console.Write("{0}  - {1} \n", t.category, t.productName);
                }
            }

            var q = from c in categories
                    join p in products on c equals p.category
                    select new { Category = c, p.productName };
            foreach (var v in q)
            {
                Console.Write("{0}  - {1} \n", v.Category, v.productName);
            }

            Console.ReadLine();
        }


    }

    public class Product
    {
        public string category { set; get; }
        public string productName { set; get; }
    }
}
